package net.oschina.app.adapter;

import android.content.res.ColorStateList;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.oschina.app.R;
import net.oschina.app.ui.MainActivity;
import net.oschina.app.util.CommonUtils;

/**
 * Created by wei03 on 2017/1/12.
 */
public class MainLeftAdapter extends BaseAdapter {
    int[] icons = new int[]{R.drawable.drawer_menu_icon_quest_nor, R.drawable.drawer_menu_icon_opensoft_nor,
            R.drawable.drawer_menu_icon_blog_nor, R.drawable.drawer_menu_icon_gitapp_nor};
    String[] names = new String[]{"技术问答", "开源中国", "博客区", "Git客户端"};
    private MainActivity mContext;

    public MainLeftAdapter(MainActivity mainActivity) {
        mContext = mainActivity;
    }

    @Override
    public int getCount() {
        return icons.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int padding = CommonUtils.px2dp(10);
        LinearLayout itemView = getItemView(padding);
        setImageView(icons[i],itemView);
        setTextView(names[i],itemView);
        return itemView;
    }

    @NonNull
    private LinearLayout getItemView(int padding) {
        LinearLayout itemView = new LinearLayout(mContext);
        itemView.setBackground(mContext.getResources().getDrawable(R.drawable.drawer_menu_item_background));
        itemView.setPadding(padding, padding, padding, padding);
        itemView.setGravity(Gravity.CENTER_VERTICAL);
        return itemView;
    }

    @NonNull
    private TextView setTextView(String name, LinearLayout itemView) {
        TextView textView = new TextView(mContext);
        textView.setText(name);
        ColorStateList colorStateList = mContext.getResources().getColorStateList(R.color.drawer_menu_text);
        textView.setTextColor(colorStateList);
        itemView.addView(textView);
        return textView;
    }

    @NonNull
    private ImageView setImageView(int icon, LinearLayout itemView) {
        ImageView imageView = new ImageView(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
        params.rightMargin = CommonUtils.px2dp(15);
        imageView.setLayoutParams(params);
        imageView.setBackgroundResource(icon);
        itemView.addView(imageView);
        return imageView;
    }
}
